﻿using System;
using RootMotion.FinalIK;
using UnityEngine;

namespace RootMotion.Demos
{
	// Token: 0x020000B2 RID: 178
	[RequireComponent(typeof(FullBodyBipedIK))]
	public class PendulumExample : MonoBehaviour
	{
		// Token: 0x0600059F RID: 1439 RVA: 0x00036380 File Offset: 0x00034580
		public void Start()
		{
			this.ik = base.GetComponent<FullBodyBipedIK>();
			Quaternion rotation = this.target.rotation;
			this.target.rotation = this.leftHandTarget.rotation;
			FixedJoint fixedJoint = this.target.gameObject.AddComponent<FixedJoint>();
			fixedJoint.connectedBody = this.leftHandTarget.GetComponent<Rigidbody>();
			this.target.GetComponent<Rigidbody>().MoveRotation(rotation);
			this.rootRelativeToPelvis = Quaternion.Inverse(this.pelvisTarget.rotation) * base.transform.rotation;
			this.pelvisToRoot = Quaternion.Inverse(this.ik.references.pelvis.rotation) * (base.transform.position - this.ik.references.pelvis.position);
			this.rootTargetPosition = base.transform.position;
			this.rootTargetRotation = base.transform.rotation;
			this.lastWeight = this.weight;
		}

		// Token: 0x060005A0 RID: 1440 RVA: 0x0003648C File Offset: 0x0003468C
		public void LateUpdate()
		{
			if (this.weight > 0f)
			{
				this.ik.solver.leftHandEffector.positionWeight = this.weight;
				this.ik.solver.leftHandEffector.rotationWeight = this.weight;
			}
			else
			{
				this.rootTargetPosition = base.transform.position;
				this.rootTargetRotation = base.transform.rotation;
				if (this.lastWeight > 0f)
				{
					this.ik.solver.leftHandEffector.positionWeight = 0f;
					this.ik.solver.leftHandEffector.rotationWeight = 0f;
				}
			}
			this.lastWeight = this.weight;
			if (this.weight <= 0f)
			{
				return;
			}
			base.transform.position = Vector3.Lerp(this.rootTargetPosition, this.pelvisTarget.position + this.pelvisTarget.rotation * this.pelvisToRoot * this.hangingDistanceMlp, this.weight);
			base.transform.rotation = Quaternion.Lerp(this.rootTargetRotation, this.pelvisTarget.rotation * this.rootRelativeToPelvis, this.weight);
			this.ik.solver.leftHandEffector.position = this.leftHandTarget.position;
			this.ik.solver.leftHandEffector.rotation = this.leftHandTarget.rotation;
			Vector3 fromDirection = this.ik.references.pelvis.rotation * this.pelvisDownAxis;
			Quaternion b = Quaternion.FromToRotation(fromDirection, this.rightHandTarget.position - this.headTarget.position);
			this.ik.references.rightUpperArm.rotation = Quaternion.Lerp(Quaternion.identity, b, this.weight) * this.ik.references.rightUpperArm.rotation;
			Quaternion b2 = Quaternion.FromToRotation(fromDirection, this.leftFootTarget.position - this.bodyTarget.position);
			this.ik.references.leftThigh.rotation = Quaternion.Lerp(Quaternion.identity, b2, this.weight) * this.ik.references.leftThigh.rotation;
			Quaternion b3 = Quaternion.FromToRotation(fromDirection, this.rightFootTarget.position - this.bodyTarget.position);
			this.ik.references.rightThigh.rotation = Quaternion.Lerp(Quaternion.identity, b3, this.weight) * this.ik.references.rightThigh.rotation;
		}

		// Token: 0x040004A4 RID: 1188
		[Range(0f, 1f)]
		[Tooltip("The master weight of this script.")]
		public float weight = 1f;

		// Token: 0x040004A5 RID: 1189
		[Tooltip("Multiplier for the distance of the root to the target.")]
		public float hangingDistanceMlp = 1.3f;

		// Token: 0x040004A6 RID: 1190
		[Tooltip("Where does the root of the character land when weight is blended out?")]
		[HideInInspector]
		public Vector3 rootTargetPosition;

		// Token: 0x040004A7 RID: 1191
		[Tooltip("How is the root of the character rotated when weight is blended out?")]
		[HideInInspector]
		public Quaternion rootTargetRotation;

		// Token: 0x040004A8 RID: 1192
		[SerializeField]
		public Transform target;

		// Token: 0x040004A9 RID: 1193
		[SerializeField]
		public Transform leftHandTarget;

		// Token: 0x040004AA RID: 1194
		[SerializeField]
		public Transform rightHandTarget;

		// Token: 0x040004AB RID: 1195
		[SerializeField]
		public Transform leftFootTarget;

		// Token: 0x040004AC RID: 1196
		[SerializeField]
		public Transform rightFootTarget;

		// Token: 0x040004AD RID: 1197
		[SerializeField]
		public Transform pelvisTarget;

		// Token: 0x040004AE RID: 1198
		[SerializeField]
		public Transform bodyTarget;

		// Token: 0x040004AF RID: 1199
		[SerializeField]
		public Transform headTarget;

		// Token: 0x040004B0 RID: 1200
		[SerializeField]
		public Vector3 pelvisDownAxis = Vector3.right;

		// Token: 0x040004B1 RID: 1201
		public FullBodyBipedIK ik;

		// Token: 0x040004B2 RID: 1202
		public Quaternion rootRelativeToPelvis;

		// Token: 0x040004B3 RID: 1203
		public Vector3 pelvisToRoot;

		// Token: 0x040004B4 RID: 1204
		public float lastWeight;
	}
}
